"""
Case Type   : 兼容M*数据库tiem函数
Case Name   : TIME函数入参为time类型，输入格式测试
Create At   : 2023.01.06
Owner       : z@wan005
Description :
    1、参数1入参为time类型hhmmss格式
    2、入参为time类型'hhmmss'格式
    3、参数1入参为time类型'hh:mm:ss'格式
    4、参数1入参为time类型，格式为time+时间
    5、参数1入参为time显式转换类型
    6、参数1入参为time类型，格式为'D hh:mm:ss'时间
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
    5、成功
    6、成功
History     :modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_time_0001"

    def test_time(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.log.info('----前置条件：创建兼容b库---')
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step1:参数1入参为time类型hhmmss格式;expect:成功--'
        self.log.info(text)
        sql_cmd = "select time(8385959);" \
                  "select time(time'245959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 8, '行数不到9行')
        self.assertEqual('838:59:59', sql_res.splitlines()[2].strip(),
                         '执行失败' + text)
        self.assertEqual('24:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

        text = "--step2:入参为time类型'hhmmss'格式;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('8385959');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('838:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = "--step3:参数1入参为time类型'hh:mm:ss'格式;expect:成功--"
        self.log.info(text)
        sql_cmd = f"select time('838:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('838:59:59',
                         sql_res.splitlines()[-2].strip(), '执行失败' + text)

        text = "--step4:参数1入参为time类型，格式为time+时间;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time(time'8385959');" \
                  "select time(time'838:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertEqual(2,
                         sql_res.count('838:59:59'), '执行失败' + text)

        text = "--step5:参数1入参为time显式转换类型;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time(cast(-8385959 as time));" \
                  "select time(235959::time);"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 9, '行数不到9行')
        self.assertIn('-838:59:59', sql_res, '执行失败' + text)
        self.assertIn('23:59:59', sql_res, '执行失败' + text)

        text = "--step6:参数1入参为time类型，格式为'D hh:mm:ss'时间;expect:成功--"
        self.log.info(text)
        sql_cmd = "select time('2 83:59:59');"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=f'{self.db_name}')
        self.log.info(sql_res)
        self.assertTrue(len(sql_res.strip().splitlines()) >= 4, '行数不到4行')
        self.assertEqual('131:59:59', sql_res.splitlines()[-2].strip(),
                         '执行失败' + text)

    def tearDown(self):
        text = '----step7:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
